package pl.javarsovia.javarsovia4android.activities;

import static pl.javarsovia.javarsovia4android.domain.ConferenceProvider.*;
import pl.javarsovia.R;
import pl.javarsovia.javarsovia4android.adapters.SpeakersListAdapter;
import pl.javarsovia.javarsovia4android.domain.Speaker;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * @author mgrzechocinski
 * @since 2010-06-13
 */
public class SpeakersActivity extends Activity {

	private SpeakersListAdapter adapter;
	private ListView speakersListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.speakers_list);

		adapter = new SpeakersListAdapter(this, getConference().getSpeakers());

		initSpeakersList();
	}

	private void initSpeakersList() {
		speakersListView = getListView();
		speakersListView.setAdapter(adapter);

		speakersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				Speaker chosenSpeaker = (Speaker) getListView().getItemAtPosition(position);
				openSpeakerDetails(chosenSpeaker);
			}

			private void openSpeakerDetails(Speaker chosenSpeaker) {
				Intent speakerDetailsIntent = new Intent(SpeakersActivity.this, SpeakerDetailsActivity.class);
				speakerDetailsIntent.putExtra(SpeakerDetailsActivity.CHOSEN_SPEAKER_KEY, chosenSpeaker);
				startActivity(speakerDetailsIntent);
			}
		});
	}

	private ListView getListView() {
		return (ListView) findViewById(R.id.speakers_list);
	}

}
